<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="30vw" destroy-on-close @closed="$emit('closed')"
             :close-on-click-modal="false"  :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
			<el-form-item label="上级模板对象" prop="pid">
				<el-cascader v-model="form.pid" :options="groups" :props="groupsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
			</el-form-item>
			<el-form-item label="模板对象名称" prop="name">
				<el-input v-model="form.name" placeholder="请输入模板对象名称" clearable autocomplete="off"></el-input>
				<div class="el-form-item-msg">名称唯一且与菜单栏名称一致，否则导致打印模板不匹配，无法选择打印模板。</div>
			</el-form-item>
			<el-form-item label="标识" prop="sign">
				<el-input v-model="form.sign" placeholder="请输入标识" clearable autocomplete="off"></el-input>
				<div class="el-form-item-msg">通过标识查找对应数据，和表单字段中表标识关联。</div>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

export default {
	components: {},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增模板对象',
				edit: '编辑模板对象',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				name:'',
				pid:0,
				sign:''
			},
			//验证规则
			rules: {
				name: [
					{ required: true, message: '请输入模板对象名称' }
				],
				sign: [
					{ required: true, message: '请输入标识' }
				],
			},
			//所需数据选项
			groups: [],
			groupsProps: {
				value: "id",
				label:'name',
				emitPath: false,
				checkStrictly: true
			}
		}
	},
	mounted() {
		this.getGroup()
	},
	methods: {
		//加载树数据
		async getGroup(){
			var res = await this.$API.print.object.all.get();
			this.groups = res.data;
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.print.object.edit.post(this.form);
					} else {
						res = await this.$API.print.object.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			// Object.assign(this.form, data)
			var reqData = {id: data.id}
			var res = await this.$API.print.object.detail.get(reqData);
			this.form.id = data.id
			this.form.name = res.data.name
			this.form.pid = res.data.pid
			this.form.sign = res.data.sign
		},
	}
}
</script>

<style lang="scss" scoped>
.el-form {
	max-height: 700px;
	overflow: hidden;
	overflow-y: auto;

}
</style>
